<?php
namespace Wcs\Upload;

use Wcs;
use Wcs\Http\PutPolicy;
use Wcs\Config;

//require_once("utils.php");
//require_once("WcsPutPolicy.class.php");
//require_once("config.inc.php");

class Uploader
{

    private $userParam;
    private $userVars;
    private $mimeType;
    function __construct($userParam, $userVars, $mimeType)
    {
        $this->userParam = $userParam;
        $this->userVars = $userVars;
        $this->mimeType = $mimeType;
    }

    /**
     * 普通上传
     * @param $bucketName
     * @param $fileName
     * @param $localFile
     * @param $returnBody
     * @return string
     */
    function upload_return($bucketName, $fileName, $localFile, $returnBody = '"fname=$(fname)&fsize=$(fsize)&url=$(url)"') {
        $pp = new PutPolicy();
        if($returnBody !== null) {
            $pp->returnBody = $returnBody;
        }
        $pp->overwrite = Config::WCS_OVERWRITE;
        $resp = $this->_upload($bucketName, $fileName, $localFile, $pp);

        $respBody = \Wcs\url_safe_base64_decode($resp->respBody);

        return $respBody;

    }

    /**
     * 回调上传
     * @param $bucketName
     * @param $fileName
     * @param $localFile
     * @param $callbackUrl
     * @param $callbackBody
     * @return array
     */
    function upload_callback($bucketName, $fileName, $localFile, $callbackUrl, $callbackBody = null) {
        $pp = new PutPolicy();
        $pp->callbackUrl = $callbackUrl;

        if($callbackBody !== null) {
            $pp->callbackBody = $callbackBody;
        }

        $pp->overwrite = Config::WCS_OVERWRITE;

        $resp = $this->_upload($bucketName, $fileName, $localFile, $pp);

        return $this->build_result($resp);
    }


    /**
     * 通知上传
     * @param $bucketName
     * @param $fileName
     * @param $localFile
     * @param $cmd
     * @param $notifyUrl
     * @return array
     */
    function upload_notify($bucketName, $fileName, $localFile, $cmd, $notifyUrl) {

        $pp = new PutPolicy();
        $pp->persistentOps = $cmd;
        $pp->persistentNotifyUrl = $notifyUrl;
        $pp->overwrite = Config::WCS_OVERWRITE;
        $resp = $this->_upload($bucketName, $fileName, $localFile, $pp);

        return $this->build_result($resp);
    }

    function _upload($bucketName, $fileName, $localFile, $putPolicy) {

        $url = Config::WCS_PUT_URL . '/file/upload';

        if ($fileName == null || $fileName === '') {
            $putPolicy->scope = $bucketName;
        } else {
            $putPolicy->scope = $bucketName . ':' . $fileName;
        }

        $token = $putPolicy->get_token(null);

        $mimeType = null;
        $fields = array(
            'token' => $token,
            'file' => $this->create_file($localFile, $this->mimeType),
            'key' => $fileName
        );

        //自定义可选参数
        if($this->userParam !== null) {
            $fields['userParam'] = $this->userParam;
        }
        if($this->userVars !== null) {
            $fields['userVars'] = $this->userVars;
        }
        if($this->mimeType !== null) {
            $fields['mimeType'] = $this->mimeType;
        }


        $resp = \Wcs\http_post($url, null, $fields);

        return $resp;
    }


    private function create_file($filename, $mime)
    {
        // PHP 5.5 introduced a CurlFile object that deprecates the old @filename syntax
        // See: https://wiki.php.net/rfc/curl-file-upload
        if (function_exists('curl_file_create')) {
            return curl_file_create($filename, $mime);
        }

        // Use the old style if using an older version of PHP
        $value = "@{$filename}";
        if (!empty($mime)) {
            $value .= ';type=' . $mime;
        }

        return $value;
    }

    private function build_result($resp) {
        if ($resp->code == 200) {
            $ret = Array(
                'code' => 200,
                'message' => 'OK'
            );
            return json_encode($ret);
        } else {
            return $resp->respBody;
        }
    }
}